package com.ajie.service.impl;


import com.ajie.entity.Comment;
import com.ajie.entity.New;
import com.ajie.mapper.CommentMapper;
import com.ajie.service.CommentService;
import com.ajie.utils.PageResult;
import com.ajie.utils.QueryInfo;
import com.ajie.utils.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CommentServiceImpl implements CommentService {
    
    @Autowired
    private CommentMapper commentMapper;
    
    
    @Override
    public Result addComment(Comment comment) {
        log.info("该文件名是：CommentServiceImpl,该方法是：addComment");
        commentMapper.addComment(comment);
        return Result.success("发送评论成功！");
    }
    
    @Override
    public Result findComment(Comment comment2) {
        log.info("该文件名是：CommentServiceImpl,该方法是：findComment");
        /*查出某类型下的所有评论*/
        List<Comment> comments = commentMapper.findComment(comment2);
        List<Comment> rootComments = comments.stream().filter(comment -> comment.getParentId() == null).collect(Collectors.toList());
        for (Comment rootComment : rootComments){
            rootComment.setChildren(comments.stream().filter(secondComment -> rootComment.getId().equals(secondComment.getParentId())).collect(Collectors.toList()));
            for (Comment secondComment :rootComment.getChildren()){
                secondComment.setChildren(comments.stream().filter(otherComment -> secondComment.getId().equals(otherComment.getParentId())).collect(Collectors.toList()));
            }
        }
//        map.put("userList",users);
        return Result.success("查看评论成功",rootComments);
    }
    
    @Override
    public Result delete(Long id) {
        log.info("该文件名是：CommentServiceImpl,该方法是：deleteComment");
        commentMapper.delete(id);
        return Result.success("删除评论成功！");
    }
    
    @Override
    public Result findAllComment(QueryInfo queryInfo) {
        PageHelper.startPage(queryInfo.getPageNumber(),queryInfo.getPageSize());
        String[] split = queryInfo.getQueryString().split("::");
        String queryString = "";
        if (!"test".equals(split[1])){
            queryString = split[1];
        }
        Page<Comment> page = commentMapper.findAllComment(split[0],queryString);
        long total = page.getTotal();
        List<Comment> result = page.getResult();
        return new PageResult(total, result);
    }
    
    @Override
    public Result updateStatus(Long id) {
        commentMapper.updateStatus(id);
        return Result.success("审核完成");
    }
}
